home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS20.ADF / BobEd / disk.c < prev    next >
C/C++ Source or Header  |  1989-01-27  |  6KB  |  199 lines

  1. #include <bobed.h>
  2. #include <lattice/fcntl.h>
  3.  
  4. /*
  5.    "disk.c"
  6.    BobEd disk functions. V1.0 uses lattice level I I/O functions.
  7. */
  8.  
  9. #define  EOF   (-1)
  10. #define  WRITEMODE   (O_CREAT | O_WRONLY) /* create if it does not exist */
  11. #define  READMODE    O_RDONLY /* read only */
  12.  
  13. int   save ();
  14. int   load ();
  15. VOID  hexword ();
  16.  
  17. extern UBYTE bobdata [OBS+1][BWIDE*16][BHIGH];
  18.  
  19. /* these definitions are for saving as c-code which can be compiled
  20.    (almost) directly. One bug to look for is the last word of data
  21.    will have a "," which C does not like to see. it can be removed
  22.    with Ed by issuing :  t;f/};/;p;p;ce;cl;dc
  23.    on the file produced by BobEd.
  24. */
  25.  
  26. /*
  27.    the char definitions below are for comments and syntax within the
  28.    c code data statements.
  29. */
  30. char  comm [] = "/* -- -- */";  /* 0-2, 10-3, 0-13 */
  31. char  come [] = " -- */";
  32. char  wide [] = "#define WIDE ";
  33. char  high [] = "#define HIGH ";
  34. char  deep [] = "#define DEEP ";
  35. char  def  [] = "SHORT name [] = {";
  36. char  edef [] = "};";
  37. char  cr   [] = {0x0a};
  38. char  s    [] = {'0','x','0','0','0','0',',','\0'};
  39. char  hex  [] = "0123456789ABCDEF";
  40. UWORD mask [] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
  41.  
  42. int   save (savec,name,v,x0,y0,x1,y1,bp)
  43. char  name [];
  44. SHORT savec,v,x0,y0,x1,y1,bp;
  45. {
  46.    int         i,x,y,p;
  47.    int         bi;   /* buffer index */
  48.    int         file;
  49.    int         len;
  50.    UWORD       val;
  51.    UBYTE       a;
  52.    UBYTE       buf [BWIDE*16];
  53.  
  54.    file = open (name, WRITEMODE);
  55.    if (file == EOF)     /* error in creating file */
  56.       return (FALSE);
  57.  
  58.    buf [0] = 0xff;      /* flag that this is bin-data not c-program */
  59.    buf [1] = x0;        /* buffer sizes for write */
  60.    buf [2] = y0;
  61.    buf [3] = x1;
  62.    buf [4] = y1;
  63.    if (savec == FALSE)  /* save as binary */
  64.    {
  65.       len = write (file, buf, 5);   /* write sizes */
  66.       bp = 1;
  67.    }
  68.  
  69.    if (savec)
  70.    {
  71.       len = write (file, comm,6);            /* write name of image  */
  72.       len = write (file, name,strlen(name));
  73.       len = write (file, come,6);
  74.       len = write (file, cr,1);
  75.       len = write (file, wide, strlen(wide));/* write word width of image */
  76.       a   = stcu_d (buf, (x1-x0+15)/16,len);
  77.       len = write (file, buf,a);
  78.       len = write (file, cr,1);
  79.       len = write (file, high, strlen(high)); /* write height of image */
  80.       a   = stcu_d (buf, y1-y0, len);
  81.       len = write (file, buf, a);
  82.       len = write (file, cr,1);
  83.       len = write (file, deep, strlen(deep)); /* write depth of image */
  84.       a   = stcu_d (buf, bp, len);
  85.       len = write (file, buf, a);
  86.       len = write (file, cr,1);
  87.       len = write (file, cr,1);
  88.       len = write (file, def,strlen(def));  /* write SHORT name [] = */
  89.       len = write (file, cr,1);
  90.    }
  91.  
  92.    for (p=0;p<bp;p++)   /* do bp planes */
  93.    {
  94.       for (y=y0;y<y1;y++)
  95.       {
  96.          if (savec)     /* save in words */
  97.          {
  98.             for (x=x0;x<x1;x+=16)
  99.             {
  100.                val=0;
  101.                for (i=0;i<16;i++)   /* one bit at a time */
  102.                {
  103.                   a = (bobdata[v][x+i][y] & mask[p])/mask[p]; /* mask bit */
  104.                   val += a*mask[15-i]; /* put into one word */
  105.                }      
  106.                hexword (val); /* convert to hex string */
  107.                len = write (file,s,7); /* write hex word */
  108.             }
  109.          }
  110.          else  /* save as binary. The method is rather inefficient but
  111.                   easy. the data is arranged as 1 byte per pixel which
  112.                   represents the color of that pixel. therefore maximum
  113.                   size of file is 3*16*45=2160 bytes. since the max colors
  114.                   in this progam is 16 it we half that size by packing
  115.                   two nibbles into each byte by shifting the second nybble
  116.                   into the upper 4 bits of the byte ( << 4).
  117.                */
  118.          {
  119.             for (x=x0,bi=0;x<x1;bi++,x+=2) /* do a line at a time, two bytes at a time */
  120.                buf [bi] = ((UBYTE)bobdata [v][x][y] | (UBYTE)bobdata [v][x+1][y] << 4);
  121.  
  122.             if (((x1-x0) % 2) > 0) /* if we went one byte too far */
  123.                buf [bi-1] = buf [bi-1] & 15;   /* blank out top bits */
  124.  
  125.             len = write (file,buf,(x1-x0)/2);
  126.          }
  127.          if (savec)
  128.             len = write (file,cr,1);   /* put a <CR> at the end of each line */
  129.       }
  130.       if (savec)  /* write a divider between planes */
  131.       {
  132.          len = write (file, comm, strlen(comm));
  133.          len = write (file, cr,1);
  134.       }
  135.    }
  136.    if (savec)  /* write the }; at EOF */
  137.    {
  138.       len = write (file, edef,2);
  139.       len = write (file, cr,1);
  140.       len = write (file, cr,1);
  141.    }
  142.    len = close (file);  /* close er up */
  143.    return (TRUE);
  144. }
  145.  
  146. VOID  hexword (w)
  147. UWORD w;
  148. {
  149.    int   i;
  150.  
  151.    for (i=0;i<4;i++)
  152.    {
  153.       s [5-i] = hex[w & 15];
  154.       w = w>>4;
  155.    }
  156. }
  157.  
  158. int   load (v,name)
  159. SHORT v;
  160. char  *name;
  161. {
  162.    SHORT x,y;
  163.    UBYTE x0,y0,x1,y1;
  164.    char  buf [5];
  165.    SHORT len;
  166.    int   file;
  167.  
  168.    file = open (name, READMODE);
  169.    if (file == EOF)
  170.       return (FALSE);
  171.  
  172.    len = read (file, buf, 5);
  173.    if (buf[0] > 0)
  174.    {
  175.       len = close (file);
  176.       return (FALSE);
  177.    }
  178.  
  179.    x0 = buf [1];
  180.    y0 = buf [2];
  181.    x1 = buf [3];
  182.    y1 = buf [4];
  183.  
  184.    for (y=y0;y<y1;y++)
  185.       for (x=x0;x<x1;x+=2)
  186.       {
  187.          len = read (file,buf,1);
  188.          bobdata [v][x][y]   = buf [0] & 15;   /* blank out top nybble */
  189.          bobdata [v][x+1][y] = (buf [0] & ~15) >> 4; /* blank bottom and
  190.                                                         shift into lower
  191.                                                         nybble */
  192.       }
  193.  
  194.    len = close (file);
  195.    return (TRUE);
  196. }
  197.  
  198.  
  199.